package com.imooc.rabbitmq;

import java.util.HashMap;
import java.util.Map;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.HeadersExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MqConfig {
	// queue
	public static final String QUEUE = "queue"; // 直连模式
	public static final String TOPIC_QUEUE1 = "topic.queue1"; // topic
	public static final String TOPIC_QUEUE2 = "topic.queue2"; // topic
	public static final String HEADER_QUEUE = "header.queue"; // header
	public static final String MIAOSHA_QUEUE = "miaosha.queue"; 
	
	//交换机exchange
	public static final String TOPIC_EXCHANGE = "topic.exchange"; // topic 交换机
	public static final String FANOUT_EXCHANGE = "fanout.exchange"; // fanout 广播交换机
	public static final String HEADER_EXCHANGE = "header.exchange"; // header 广播交换机
	
	//路由规则routing key
	public static final String ROUTING_KEY1 = "topic.key1"; // ROUTING_KEY1
	public static final String ROUTING_KEY2 = "topic.#"; // ROUTING_KEY2

	/**
	 * Direct（直连）模式 
	 * @return
	 */
	@Bean
	public Queue miaoshaQueue(){
		return new Queue(MIAOSHA_QUEUE, true);
	}
	
	/**
	 * Direct（直连）模式 
	 * @return
	 */
	@Bean
	public Queue queue(){
		return new Queue(QUEUE, true);
	}
	
	/**
	 * Topic模式 
	 * @return
	 */
	@Bean
	public Queue topicQueue1(){
		return new Queue(TOPIC_QUEUE1, true);
	}
	
	/**
	 * Topic模式 
	 * @return
	 */
	@Bean
	public Queue topicQueue2(){
		return new Queue(TOPIC_QUEUE2, true);
	}
	
	/**
	 * header模式 
	 * @return
	 */
	@Bean
	public Queue headerQueue(){
		return new Queue(HEADER_QUEUE, true);
	}
	
	
	
	/**
	 * Topic交换机
	 * @return
	 */
	@Bean
	public TopicExchange topicExchange(){
		return new TopicExchange(TOPIC_EXCHANGE);
	}
	
	/**
	 * 广播交换机
	 * @return
	 */
	@Bean
	public FanoutExchange fanoutExchange(){
		return new FanoutExchange(FANOUT_EXCHANGE);
	}
	
	/**
	 * header模式交换机
	 * @return
	 */
	@Bean
	public HeadersExchange headerExchange(){
		return new HeadersExchange(HEADER_EXCHANGE);
	}
	
	@Bean
	public Binding fanoutBind1(){
		return BindingBuilder.bind(topicQueue1()).to(fanoutExchange());
	}
	
	@Bean
	public Binding fanoutBind2(){
		return BindingBuilder.bind(topicQueue2()).to(fanoutExchange());
	}
	
	
	
	@Bean
	public Binding topicBinding1(){
		return BindingBuilder.bind(topicQueue1()).to(topicExchange()).with("topic.key1");
	}
	
	@Bean
	public Binding topicBinding2(){
		return BindingBuilder.bind(topicQueue2()).to(topicExchange()).with("topic.#");
	}
	
	
	@Bean
	public Binding headerBinding(){
		Map<String, Object> headerKeys = new HashMap<String, Object>();
		headerKeys.put("header1", "value1");
		headerKeys.put("header2", "value2");
		return BindingBuilder.bind(headerQueue()).to(headerExchange()).whereAll(headerKeys).match();
	}
	
	
}
